package com.example.service.Impl;

import com.example.entity.Video;
import com.example.mapper.VideoMapper;
import com.example.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public Map<String, Object> getVideosByPage(Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Video> videos = videoMapper.selectVideosByPage(offset, pageSize);
        int total = videoMapper.countVideos();
        Map<String, Object> result = new HashMap<>();
        result.put("data", videos);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    @Override
    public Map<String, Object> searchVideos(Integer pageNum, Integer pageSize, String titlePart) {
        int offset = (pageNum - 1) * pageSize;
        List<Video> videos = videoMapper.selectVideosByTitlePart(offset, pageSize, titlePart);
        int total = videoMapper.countVideosByTitlePart(titlePart);
        Map<String, Object> result = new HashMap<>();
        result.put("data", videos);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }
}
